/**
 * 离线收益界面
 */
class OfflineUI extends UIBase{
    private btn_double:BaseButton;
    private btn_get:BaseButton;
    private btn_close:BaseButton;
    private lb_count_1:eui.Label;
    private lb_count_2:eui.Label;

    constructor()
    {
        super('OfflineView',{showBG:true});
    }

    public init()
    {
        super.init(true);
        let money = RoleCache.instance().offline_money;
        this.lb_count_2.text = money+'';
        this.lb_count_1.text = money * 2+'';
    }

    public addEvent()
    {
        this.btn_double.addTapHandler(this.doubleHandle, this);
        this.btn_get.addTapHandler(this.getHandle, this);
        this.btn_close.addTapHandler(this.close, this);
        GameUtil.addEventListener(EventConst.SHARE_SUCCESS, this.shareSuccess, this);
    }

    protected doubleHandle()
    {
        // SharePrase.instance().requestOneAdvertisement({type:ShareId.offline});
        let self = this;
        let Success_call = function()
        {  
            // self.addTickets('video');
            self.getOfflineAward(2, this.close, this);
        };
        let Error_call = function()
        {  
            MessageUtil.tips('观看完整视频才能双倍收益哦！');
        };
        WeChatgGameManager.instance().showVideoAd(GameConst.adList.other_vedio, Success_call, Error_call, this);
    }

    protected shareSuccess(e:BaseEvent)
    {
        let type = e.param['type'];

        if(type == ShareId.offline)
        {
            this.getOfflineAward(2, this.close, this);
        }
    }

    protected getHandle()
    {
        this.getOfflineAward(1,this.close, this);
    }

    protected close()
    {
        this.remove(1);
    }

    public removeEvent()
    {
        this.btn_double.removeTapHandler();
        this.btn_get.removeTapHandler();
        GameUtil.removeEventListener(EventConst.SHARE_SUCCESS, this.shareSuccess, this);
    }

    public clear()
    {
        super.clear();
        RoleCache.instance().offline_money = 0; 
    }

    public getOfflineAward(multiple, call:Function, scope)
    {
        let data = {
            multiple:multiple,
            version:GameConst.version
        }
        
        HttpManager.instance().sendPost(GameConst.httpUrl + 'warrioruser/get_offline_money', StringUtil.jsonToHttpPostString(data), (e)=>{
            let data = JSON.parse(e);
            if(data.msg == 'ok')
            {
                let award = data.data;
                if(call)
                {
                    call.call(scope);
                }
                GameUtil.openAwardUI(award, null, null);
            }
            else
            {
                MessageUtil.tips('领取失败!');
            }
        }, this);
    }
}